﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Net;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace Homework10
{
    public partial class Form1 : Form
    {
        private delegate void MyDelegate();
        public Form1()
        {
            InitializeComponent();
        }

        public void search_from_baidu(string url)
        {
            try
            {
                WebClient wb_baidu = new WebClient();
                byte[] data = wb_baidu.DownloadData(url);
                string response = Encoding.UTF8.GetString(data);
                response = Regex.Replace(response, @"[^\u4e00-\u9fa5]", "");
                if (response.Length < 200)
                {
                    this.BeginInvoke(new MyDelegate(() => { textBox_baidu.Text = response; }));
                }
                else
                {
                    this.BeginInvoke(new MyDelegate(() => { textBox_baidu.Text = response.Substring(0, 200); }));
                }

            }
            catch (Exception)
            {
                MessageBox.Show("百度获取失败！");
            }



        }
        public void search_from_bing(string url)
        {
            try
            {
                WebClient wb_bing = new WebClient();
                byte[] data = wb_bing.DownloadData(url);
                string response = Encoding.UTF8.GetString(data);
                response = Regex.Replace(response, @"[^\u4e00-\u9fa5]", "");
                if (response.Length < 200)
                {
                    this.BeginInvoke(new MyDelegate(() => { textBox_bing.Text = response; }));
                }
                else
                {
                    this.BeginInvoke(new MyDelegate(() => { textBox_bing.Text = response.Substring(0, 200); }));
                }

            }
            catch (Exception)
            {
                MessageBox.Show("必应获取失败！");
            }


        }
        public void search_from_360(string  url)
        {
            try
            {
                WebClient wb_360 = new WebClient();
                byte[] data = wb_360.DownloadData(url);
                string response = Encoding.UTF8.GetString(data);
                response = Regex.Replace(response, @"[^\u4e00-\u9fa5]", "");
                if (response.Length < 200)
                {
                    this.BeginInvoke(new MyDelegate(() => { textBox_360.Text = response; }));
                }
                else
                {
                    this.BeginInvoke(new MyDelegate(() => { textBox_360.Text = response.Substring(0, 200); }));
                }
            }
            catch (Exception)
            {
                MessageBox.Show("360获取失败！");
            }

        }
        private void enter_button_Click(object sender, EventArgs e)
        {
            Task search_baidu = new Task(() => { search_from_baidu("https://www.baidu.com/?wd=" + textBox_search.Text); });
            Task search_bing = new Task(() => { search_from_bing("https://cn.bing.com/?q=" + textBox_search.Text); });
            Task search_360 = new Task(() => { search_from_360("https://www.so.com/?q=" + textBox_search.Text); });
            search_baidu.Start();
            search_bing.Start();
            search_360.Start();
        }
    }
}
